//
//  DUNotification.m
//  APP
//
//  Created by duyujin1 on 2021/9/1.
//

#import "DUNotification.h"
#import <UserNotifications/UserNotifications.h>

@interface DUNotification()<UNUserNotificationCenterDelegate>

@end

@implementation DUNotification

+(DUNotification *)notificatonManager{
    static DUNotification* notification;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        notification=[[DUNotification alloc] init];
    });
    return notification;
}

-(void)checkNotificationAuthorization{
    UNUserNotificationCenter * center=[UNUserNotificationCenter currentNotificationCenter];
    center.delegate=self;
    [center requestAuthorizationWithOptions:UNAuthorizationOptionBadge|UNAuthorizationOptionAlert completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if(granted){
            //本地推送
            [self _pushNotificationCenter];
        }
    }];
}

-(void)_pushNotificationCenter{
    UNMutableNotificationContent *content=[[UNMutableNotificationContent alloc] init];
    content.badge=@(1);
    content.title=@"京东科技";
    content.body=@"京东科技的员工这么评价京东……";
    content.sound=[UNNotificationSound defaultSound];
    UNTimeIntervalNotificationTrigger * trigger=[UNTimeIntervalNotificationTrigger triggerWithTimeInterval:20 repeats:NO];
    UNNotificationRequest * request=[UNNotificationRequest requestWithIdentifier:@"_pushNotificationCenter" content:content trigger:trigger];
    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
        NSLog(@"");
    }];
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
    completionHandler(UNNotificationPresentationOptionAlert);
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
    //点击推送的业务逻辑
    completionHandler();
}

@end
